#http://stackoverflow.com/questions/1927660/compare-two-images-the-python-linux-way
def compare_file(image1path, image2path):
    import Image
    import math
    import operator
    __docstring__ = "compare two image files"
    h1 = Image.open(image1path).histogram()
    h2 = Image.open(image2path).histogram()
    rms = math.sqrt(reduce(operator.add, map(lambda a,b: (a-b)**2, h1, h2))/len(h1))
    print("RMS = %f" % rms)
    return True if rms <= 12 else False

# compare two image objects
# condition: Image is already imported
def compare_images(image1, image2):
    import Image
    import math
    import operator
    h1 = image1.histogram()
    h2 = image2.histogram()
    rms = math.sqrt(reduce(operator.add, map(lambda a,b: (a-b)**2, h1, h2))/len(h1))
    return True if rms <= 20 else False

# find the block type from a set of block chunks of type Image
# chunk must use image_load for transparency
def get_block_type(chunk, block_images):
    histogram = chunk.histogram()
    for block in block_images:
        if compare_images(chunk, block):
            return block.filename
    return False
